package com.aracky.assess.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.aracky.assess.constants.ComConstants;
import com.aracky.assess.model.excel.KpiUserHeaderExport;
import com.aracky.assess.model.form.HrKpiUserPerformanceHeaderTimeUpdateForm;
import com.aracky.assess.model.request.HrKpiUserPerformanceHeaderRequest;
import com.aracky.assess.model.vo.HrKpiUserPerformanceHeaderVO;
import com.aracky.assess.service.HrKpiUserPerformanceHeaderService;
import com.aracky.assess.util.ExcelFillCellMergeStrategy;
import com.aracky.common.annotation.DataDeal;
import com.aracky.common.util.ComUtil;
import com.aracky.common.util.FileUtil;
import com.aracky.common.util.UserIPUtil;
import com.aracky.common.vo.CommonResult;
import com.aracky.common.vo.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.ByteArrayOutputStream;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户绩效考核 前端控制器
 * </p>
 *
 * @author zzp
 * @since 2022-08-01 09:43:18
 */
@RestController
@RequestMapping("/hrKpiUserPerformanceHeader")
@Api(tags = "用户权重详情接口")
public class HrKpiUserPerformanceHeaderController {
    @Resource
    private HrKpiUserPerformanceHeaderService hrKpiUserPerformanceHeaderService;

    /**
     * 根据用户id或者跟据主键id，修改填报、评分日期
     * @param hrKpiUserPerformanceHeaderTimeUpdatefForm
     * @return
     */
    @PostMapping("updateEndWriteAssessorTime")
    @ApiOperation(value = "修改填报、评分日期")
    public CommonResult updateEndWriteAssessorTime(@RequestBody HrKpiUserPerformanceHeaderTimeUpdateForm hrKpiUserPerformanceHeaderTimeUpdatefForm){
        try {
            hrKpiUserPerformanceHeaderService.updateEndWriteAssessorTime(hrKpiUserPerformanceHeaderTimeUpdatefForm);
        } catch (Exception e) {
            return CommonResult.error(e.toString());
        }
        return CommonResult.success("修改成功！");
    }

    @PostMapping("/getUserHeaderList")
    @ApiOperation("员工考核列表")
    @DataDeal(user = true, dict = true)
    public CommonResult<PageVO<HrKpiUserPerformanceHeaderVO>> getUserHeaderList(@RequestBody HrKpiUserPerformanceHeaderRequest hrKpiUserPerformanceHeaderRequest) {
        //判断当前用户信息的角色是不是HR
        List<String> currentRoleKey = UserIPUtil.getCurrentRoleKey();
        if (ComUtil.isNullList(currentRoleKey) || !currentRoleKey.contains(ComConstants.ROLE.ROLE_HR)) {
            //赋值登陆人id给考核人
            hrKpiUserPerformanceHeaderRequest.setAssessorId(UserIPUtil.getCurrentUserId());
        }
        PageVO<HrKpiUserPerformanceHeaderVO> pageVO = hrKpiUserPerformanceHeaderService.getHrKpiUserPerformanceHeaderList(hrKpiUserPerformanceHeaderRequest);

        return CommonResult.success(pageVO);
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除")
    @ApiImplicitParam(name = "ids", value = "表id")
    public CommonResult deleteInfo(Long[] ids) {
        List<Long> list = new ArrayList<>();
        for (Long id : ids) {
            list.add(id);
        }
        hrKpiUserPerformanceHeaderService.removeBatchByIds(list);
        return CommonResult.success("删除成功!");
    }

    @GetMapping("/{userId}")
    @ApiOperation(value = "通过用户id查看详情")
    public CommonResult<HrKpiUserPerformanceHeaderVO> getInfo(@PathVariable Long userId) {
        HrKpiUserPerformanceHeaderVO hrKpiUserPerformanceHeaderVO = hrKpiUserPerformanceHeaderService.getHeaderInfo(userId);
        return CommonResult.success(hrKpiUserPerformanceHeaderVO, "查询详情成功!");
    }

    /**
     * 通过id查询详情
     * @param id
     * @return
     */
    @GetMapping("/getById")
    @ApiOperation(value = "通过id查看详情")
    public CommonResult<HrKpiUserPerformanceHeaderVO> getById(@RequestParam Long id) {
        HrKpiUserPerformanceHeaderVO hrKpiUserPerformanceHeaderVO = hrKpiUserPerformanceHeaderService.myGetById(id);
        if (null == hrKpiUserPerformanceHeaderVO) {
            return CommonResult.error("查询详情失败！");
        }
        return CommonResult.success(hrKpiUserPerformanceHeaderVO, "查询详情成功!");
    }


    @PostMapping("/export")
    @ApiOperation(value = "导出考核管理信息")
    public ResponseEntity<byte[]> exportData(@RequestBody HrKpiUserPerformanceHeaderRequest hrKpiUserPerformanceHeaderRequest)  {
        //判断当前用户信息的角色是不是HR
        List<String> currentRoleKey = UserIPUtil.getCurrentRoleKey();
        if (ComUtil.isNullList(currentRoleKey) || !currentRoleKey.contains(ComConstants.ROLE.ROLE_HR)) {
            //赋值登陆人id给考核人
            hrKpiUserPerformanceHeaderRequest.setAssessorId(UserIPUtil.getCurrentUserId());
        }
        //查询数据信息
        List<KpiUserHeaderExport> exportList = hrKpiUserPerformanceHeaderService.getExportDataList(hrKpiUserPerformanceHeaderRequest);
        //写入流
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int[] mergeColumnIndex=new int[]{0,1,2,3,4,5,6,7,8,9,10,16,18,19,20,22,23,24};
        ExcelWriter excelWriter = EasyExcel.write(baos, KpiUserHeaderExport.class).build();
        ExcelFillCellMergeStrategy strategy=new ExcelFillCellMergeStrategy(1,mergeColumnIndex);
        WriteSheet writeSheet = EasyExcel.writerSheet("考核管理").build();
        excelWriter.write(exportList,writeSheet);
        excelWriter.finish();
//        EasyExcel.write(baos, KpiUserHeaderExport.class).autoCloseStream(Boolean.FALSE).sheet("考核管理")
//                .doWrite(exportList);
        String fileName = LocalDate.now() + "考核管理.xlsx";
        return FileUtil.downloadFile(fileName, baos);
    }


}
